<?php
/**
 * Created by PhpStorm.
 * User: Administrator--Reer
 * Date: 2019/9/9 0009
 * Time: 17:16
 */

namespace app\store\controller;

use app\common\bean\GlobalBean;
use app\common\bean\StoreBean;
use app\common\bean\StoreRolePermissionBean;
use app\common\exception\MyException;
use app\common\manage\StoreManage;
use app\common\manage\StoreRolePermissionManage;
use app\common\utils\CurlUtil;
use think\App;
use think\Controller;

class Index extends Controller
{
    /**
     * Index constructor.
     * @param App|null $app
     * @throws \Exception
     */
    public function __construct(App $app = null){
        parent::__construct($app);
        if(!session(GlobalBean::$storeId)){
            MyException::throwMyException(GlobalBean::$goLogin);
        }
    }

    /** 首页渲染
     * @return mixed
     * @throws \Exception
     */
    public function index(){
        $storeRolePermissionBean = new StoreRolePermissionBean();
        $storeRolePermissionBean->setstoreRoleId(session(GlobalBean::$storeRoleId));
        $permissionTree = StoreRolePermissionManage::getInstance()->getStoreRolePermissionTree($storeRolePermissionBean);
        $this->assign('permissionTree',$permissionTree);
        return $this->fetch();
    }

    public function welcome(){
        $storeBean = new StoreBean();
        $storeBean->setId(session(GlobalBean::$storeId));
        $store = StoreManage::getInstance()->getInfo($storeBean);
        $store_id = session(GlobalBean::$storeId);
        $img_url = "upload/storeQrcode/store_id={$store_id}.png";
        if(!is_file($img_url)){
            $base_img = CurlUtil::getInstance()->getQrcode($store_id);
            file_put_contents($img_url, $base_img);
        }
        $this->assign([
            'store'=>$store->getData()
        ]);
        $this->assign( compact('img_url') );
        return $this->fetch();
    }
}